package org.opu.vcforms.template.bean;

import java.util.Collection;
import java.util.Properties;
import java.util.Set;
import org.opu.doc2db.document.datadocument.ParagraphList;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

/**
 *  In general, it is a double array
 * @author yura
 */
@Root(name = "content")
public class Content extends ParagraphList {

    /** 
     * Database - in this case data will be getting from database
     */
    public static final String TYPE_DATABASE = "database";
    /**
     * User settings - in this case data will be getting from
     * enviroment where from it`s tunning
     */
    public static final String TYPE_USER_SETTINGS = "user-settings";

    /**
     * User settings - in this case data don`t be retruned, this
     * is preferences for transform program
     */
    public static final String TYPE_SYSTEM_SETTINGS = "system-settings";
    /**
     * Sql - if type was {@link #TYPE_DATABASE}, this is query for
     * getting data
     */
    public static final String PROPERYY_SQL = "sql";
    /**
     * Inject - if type was {@link #TYPE_USER_SETTINGS}, this is
     * an indication of the desired data
     */
    public static final String PROPERYY_SETTINGS_TYPE = "inject";
    
    /** 
     *  Get last result of data getting of content '#'
     */
    public static final char CALL_GET = '#';

    /**
     * Cause next of content.
     * <br/><b>Note:</b>Available only for content '?'
     */
    public static final char CALL_NEXT = '?';

    /**
     * turn to upper content (super), does not make sense alone, use
     * it with {@link #CALL_GET} or {@link #CALL_NEXT}.  '-'
     */
    public static final char CALL_SUPER = '-';

    @ElementMap(entry = "property", key = "key", attribute = true,
            keyType = String.class, valueType = String.class)
    private Properties properties;
    @Attribute
    private String type;

    public Content() {
        this("");
    }

    public Content(Collection elements) {
        this(elements, null, "");
    }

    public Content(String type) {
        this(null, null, type);
    }

    public Content(Collection elements, Properties properties, String type) {
        super(elements);
        if (properties == null) {
            properties = new Properties();
        }
        this.properties = properties;
        this.type = type;
    }

    /**
     *  Set some specific properties.
     * @param name property name
     * @param value propertie value
     */
    public void set(String name, String value) {
        properties.setProperty(name, value);
    }

    /**
     *  Return some specific properties.
     * @param name property name
     * @return some specific properties.
     */
    public String get(String name) {
        return properties.getProperty(name);
    }

    /**
     *  Return true if this content has no properties; false otherwise.
     * @return true if this content has no properties; false otherwise.
     */
    public boolean isEmpty() {
        return properties.isEmpty();
    }

    /**
     *  Return a set of keys in this property list
     * @return a set of keys in this property list
     */
    public Set<String> propertyNames() {
        return properties.stringPropertyNames();
    }

    /**
     *  Return type of content. The type can be
     * only database, for now
     * @return type of content
     */
    public String getType() {
        return type;
    }

    /**
     *  Set variable type. The type can be
     * only database, for now
     * @param type type of content
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * Return current properties
     * @return  current properties
     * @deprecated It`s only for simplt-xml !
     * Use {@link #get(java.lang.String)},
     * {@link #set(java.lang.String, java.lang.String) } and
     * {@link #propertyNames()}
     */
    public Properties getProperties() {
        return properties;
    }

    /**
     * Set new properties.
     * @param properties new value
     * @deprecated It`s only for simplt-xml !
     * Use {@link #get(java.lang.String)},
     * {@link #set(java.lang.String, java.lang.String) } and
     * {@link #propertyNames()}
     */
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
